package com.whw.fy.service.impl;

import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.ListUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.whw.fy.service.CourseSummaryService;

@Service
public class CourseSummaryServiceImpl implements CourseSummaryService {

    @Resource
    private CourseSummaryMapper courseSummaryMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ClazzMapper clazzMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Message deleteByPrimaryKey(Integer courseSummaryId) {
        return courseSummaryMapper.deleteByPrimaryKey(courseSummaryId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(CourseSummary record) {
        return courseSummaryMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer courseSummaryId) {
        CourseSummary courseSummary = courseSummaryMapper.selectByPrimaryKey(courseSummaryId);
        return Message.success(null).add(initCourseSummary(courseSummary));
    }

    @Override
    public Message updateByPrimaryKeySelective(CourseSummary record) {
        return courseSummaryMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message selectAllByCourseSummaryStudentId(Integer courseSummaryStudentId) {
        List<CourseSummary> courseSummaryList = courseSummaryMapper.selectAllByCourseSummaryStudentId(courseSummaryStudentId);
        for (CourseSummary courseSummary : courseSummaryList)
            courseSummary = initCourseSummary(courseSummary);
        return Message.success(null).add(courseSummaryList);
    }

    @Override
    public Message selectAllByCourseSummaryClazzId(Integer courseSummaryClazzId) {
        List<CourseSummary> courseSummaryList = courseSummaryMapper.selectAllByCourseSummaryClazzId(courseSummaryClazzId);
        for (CourseSummary courseSummary : courseSummaryList)
            courseSummary = initCourseSummary(courseSummary);
        return Message.success(null).add(courseSummaryList);
    }


    @Override
    public Message updateBatchSelective(List<CourseSummary> list) {
        return courseSummaryMapper.updateBatchSelective(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message batchInsert(List<CourseSummary> list) {
        return courseSummaryMapper.batchInsert(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectallByTeacherIdJurisdictionEight(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassTeacherId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz : clazzList) {
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<CourseSummary> courseSummaryList = new ArrayList<>();

        for (Integer id : clazzIdList) {
            List<CourseSummary> courseSummaryList1 = courseSummaryMapper.selectAllByCourseSummaryClazzId(id);
            courseSummaryList.addAll(courseSummaryList1);
        }
        for (CourseSummary courseSummary : courseSummaryList)
            courseSummary = initCourseSummary(courseSummary);
        return Message.success(null).add(courseSummaryList);
    }

    @Override
    public Message selectallByMedicalDirectorIdJurisdictionZero(int staffId) {
        List<CourseSummary> courseSummaryList = courseSummaryMapper.selectAllByCourseSummaryMedicalDirectorId(staffId);
        for (CourseSummary courseSummary : courseSummaryList) {
            courseSummary = initCourseSummary(courseSummary);
        }
        return Message.success(null).add(courseSummaryList);
    }

    @Override
    public Message selectallByprincipalTeacherJurisdictionSeven(int staffId) {
        List<Clazz> clazzList = clazzMapper.selectByClassResponsibleTeacherId(staffId);
        List<Integer> clazzIdList = new LinkedList<>();
        for (Clazz clazz : clazzList) {
            clazzIdList.add(clazz.getClassId());
        }
        clazzIdList = ListUtil.duplicate(clazzIdList);
        List<CourseSummary> courseSummaryList = new ArrayList<>();

        for (Integer id : clazzIdList) {
            List<CourseSummary> courseSummaryList1 = courseSummaryMapper.selectAllByCourseSummaryClazzId(id);
            courseSummaryList.addAll(courseSummaryList1);
        }
        for (CourseSummary courseSummary : courseSummaryList)
            courseSummary = initCourseSummary(courseSummary);
        return Message.success(null).add(courseSummaryList);
    }

    @Override
    public Message selectallByTeachingDirectorJurisdictionSix(int staffId) {
        List<CourseSummary> courseSummaryList = courseSummaryMapper.selectByCourseSummaryTeachingSupervisorId(staffId);
        for (CourseSummary courseSummary : courseSummaryList) {
            courseSummary = initCourseSummary(courseSummary);
        }
        return Message.success(null).add(courseSummaryList);
    }


    @Override
    public Message selectAll() {
        List<CourseSummary> courseSummaryList = courseSummaryMapper.selectAll();
        for (CourseSummary courseSummary : courseSummaryList)
            courseSummary = initCourseSummary(courseSummary);
        return Message.success(null).add(courseSummaryList);
    }


    private CourseSummary initCourseSummary(CourseSummary courseSummary) {
        //设置学生姓名
        if (courseSummary.getCourseSummaryStudentId() != null) {
            courseSummary.setCourseSummaryStudentName(studentMapper.selectByPrimaryKey(courseSummary.getCourseSummaryStudentId()).getStudentName());
        }
        //设置坑成类型和技术类别
        if (courseSummary.getCourseSummaryClazzId() != null) {
            Clazz clazz = clazzMapper.selectByPrimaryKey(courseSummary.getCourseSummaryClazzId());
            Lesson lesson = lessonMapper.selectByPrimaryKey(clazz.getClassCourseId());
            courseSummary.setCourseSummarytechnicalTypeName(lesson.getTechnicalType().getTechnicalTypeName());
            courseSummary.setCourseSummaryclazzTypeName(clazz.getClazz_type().getClazzTypeName());
        }
        //设置授课老师名称
        if (courseSummary.getCourseSummaryTeacherId() != null)
            courseSummary.setCourseSummaryPeopleTeacherName(staffMapper.selectByPrimaryKey(courseSummary.getCourseSummaryTeacherId()).getStaffName());
        //设置教学主管名称
        if (courseSummary.getCourseSummaryTeachingSupervisorId() != null)
            courseSummary.setCourseSummaryTeachingSupervisorName(staffMapper.selectByPrimaryKey(courseSummary.getCourseSummaryTeachingSupervisorId()).getStaffName());
        //设置医疗主管名称
        if (courseSummary.getCourseSummaryMedicalDirectorId() != null)
            courseSummary.setCourseSummaryMedicalDirectorName(staffMapper.selectByPrimaryKey(courseSummary.getCourseSummaryMedicalDirectorId()).getStaffName());

        return courseSummary;
    }

    @Override
    public int insert(CourseSummary record) {
        return courseSummaryMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(CourseSummary record) {
        return courseSummaryMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message updateReplyTeachingDirector(CourseSummary courseSummary) {
        return courseSummaryMapper.updateByPrimaryKeySelective(courseSummary) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message updateReplyPrincipalTeacher(CourseSummary courseSummary) {
        return courseSummaryMapper.updateByPrimaryKeySelective(courseSummary) > 0 ? Message.success(null) : Message.fail(null);
    }
}

